/**
 * PlayerId.java
 *
 * @author Beatrice Bacelli
 * @author Luca Bedogni
 * @author Silvia Righini
 */
package unibo.lsb.logic;

import java.io.Serializable;
import java.util.Random;

import unibo.lsb.graphic.ConfigurationConstGraphics;
import unibo.lsb.server.ConfigurationConstants;

/**
 * This class is supposed to be used by the server in the registration phase. A
 * player here is characterized by name, ip address, port and icon number. This
 * class is also included in the class {@link Player}
 * 
 */
public class PlayerId implements Serializable {

	String name;
	String password;
	String ipAddress;
	String iconNumber;
	int port;

	/**
	 * Constructor.
	 * 
	 * @param name
	 * @param password
	 * @param ipAddress
	 * @param port
	 * @param iconNumber
	 */
	public PlayerId(String name, String password, String ipAddress, int port,
			String iconNumber) {
		super();
		this.name = name;
		setSafePassword(password);
		this.ipAddress = ipAddress;
		this.iconNumber = iconNumber;
		this.port = port;
	}

	/**
	 * Constructor.
	 * 
	 * @param name
	 * @param password
	 * @param ipAddress
	 * @param iconNumber
	 */
	public PlayerId(String name, String password, String ipAddress,
			String iconNumber) {
		super();
		this.name = name;
		setSafePassword(password);
		this.ipAddress = ipAddress;
		this.iconNumber = iconNumber;
		this.port = ConfigurationConstants.RMIREGISTRYPORTINFORMATION;
	}

	/**
	 * Constructor.
	 * 
	 * @param name
	 * @param password
	 * @param ipAddress
	 */
	public PlayerId(String name, String password, String ipAddress) {
		super();
		this.name = name;
		setSafePassword(password);
		this.ipAddress = ipAddress;
		this.port = ConfigurationConstants.RMIREGISTRYPORTINFORMATION;

		this.iconNumber = generateRandomIcon();
	}

	/**
	 * Constructor.
	 * 
	 * @param name
	 * @param password
	 * @param ipAddress
	 * @param port
	 */
	public PlayerId(String name, String password, String ipAddress, int port) {
		super();
		this.name = name;
		setSafePassword(password);
		this.ipAddress = ipAddress;
		this.port = port;

		this.iconNumber = generateRandomIcon();
	}

	/**
	 * Set password.
	 * 
	 * @param password
	 */
	private void setSafePassword(String password) {
		if (password == null) {
			this.password = "";
		} else {
			this.password = password;
		}

	}

	/**
	 * Generates a random valid icon number. The number is no greater than
	 * {@link ConfigurationConstGraphics#ICON_END}.
	 * 
	 * @return
	 */
	private static int randomIcon() {
		Random seed = new Random();
		int randomNumber = seed.nextInt(ConfigurationConstGraphics.ICON_END) + 1;
		return randomNumber;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the ipAddress
	 */
	public String getIpAddress() {
		return ipAddress;
	}

	/**
	 * @param ipAddress
	 *            the ipAddress to set
	 */
	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		setSafePassword(password);
	}

	public String getIconNumber() {
		return iconNumber;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public int getPort() {
		return port;
	}

	public void setIconNumber(String iconNumber) {
		this.iconNumber = iconNumber;
	}

	/**
	 * Ad-hoc equals
	 * 
	 * @param p
	 * @return
	 */
	public boolean equals(PlayerId p) {
		boolean b = false;
		if ((this.getName().equals(p.getName()))
				&& (this.getIpAddress().equals(p.getIpAddress()))
				&& (this.getPassword().equals(p.getPassword()))
				&& (this.getPort() == (p.getPort()))
				&& (this.getIconNumber().equals(p.getIconNumber()))) {
			b = true;
		}
		return b;
	}

	@Override
	public String toString() {
		String value;
		if (password.length() == 0) {
			value = ipAddress + ":" + port + "." + name + "." + ".icon"
					+ iconNumber;
		} else {
			value = ipAddress + ":" + port + "." + name + "." + password + "."
					+ ".icon" + iconNumber;
		}
		return value;
	}

	/**
	 * Randomly chooses a valid icon for the player, and sets it.
	 * 
	 * @return
	 */
	public static String generateRandomIcon() {
		String iconN = (new Integer(randomIcon())).toString();
		int iconNlength = iconN.length();
		if (iconNlength < ConfigurationConstGraphics.ICON_NAME_LENGTH) {
			String stringToAdd = "";
			for (int i = 0; i < (ConfigurationConstGraphics.ICON_NAME_LENGTH - iconNlength); i++) {
				stringToAdd = stringToAdd + "0";
			}
			iconN = stringToAdd + iconN;
		}
		return iconN;
	}
}
